/**
 * @(#) MainMediator.as
 *
 * @date		2009. 02. 05.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	본인 인사정보 화면 Mediator
 * 				
 */
package com.skt.common_hr.main.view
{
	import com.skt.common_hr.common.*;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.main.ApplicationFacade;
	import com.skt.common_hr.main.events.AutoLogoutViewEvent;
	import com.skt.common_hr.main.model.CountryProxy;
	import com.skt.common_hr.main.model.MyInfoProxy;
	import com.skt.common_hr.main.view.components.AutoLogoutView;
	import com.wcl.utils.ExternalCommand;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	public class MainMediator extends OverViewMediator
	{
		public static const NAME:String = "MainMediator";
		
		private var _moduleManager:AppModuleManager;
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function MainMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 
		/**
		* View를 초기화 한다.
		*	- Event Handler를 설정한다.
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
 			if(Variables.timeout > 0 ) {
	 			Variables.safeTimer = new Timer( Variables.timeout * 60 * 1000); 
	 			Variables.safeTimer.addEventListener(TimerEvent.TIMER, onSafeTimeOut);
 			}
 			var proxy:MyInfoProxy = facade.retrieveProxy(MyInfoProxy.NAME) as MyInfoProxy;
 			proxy.load(Variables.myPernr);
 			
 			//var countryProxy:CountryProxy = facade.retrieveProxy(CountryProxy.NAME) as CountryProxy;
 			//countryProxy.load();
 			
 			_moduleManager = new AppModuleManager(view.vsModules);
 			_moduleManager.addEventListener(AppModuleManager.ERROR_LOAD_MODULE, onErrorLoadModule);
 		}







		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					Messages.EXECUTE_MODULE,
					Messages.LOGOUT,
					CountryProxy.LOAD_COUNTRY_COMPLETE,
					ApplicationFacade.LOAD_PROFILE_COMPLETE,
					ApplicationFacade.LOAD_PROFILE_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case Messages.EXECUTE_MODULE:
					_moduleManager.executeModule(note.getBody() as String);
					break;
				case Messages.LOGOUT:
					trace("logout");
					ExternalCommand.closeBrowser();
					break;
				case ApplicationFacade.LOAD_PROFILE_COMPLETE:
					storeProfile(note.getBody() as MyInfoProxy);
					
					if(view.startModule != null && view.startModule != "" && view.startModule != "null") {
						view.home.selected_menu(view.startModule);	
					}
					break;
				case ApplicationFacade.LOAD_PROFILE_FAILED:
					trace(MyInfoProxy(note.getBody()).faultMessage);
					onFailedLoadProfile(MyInfoProxy(note.getBody()));
					break;
				case CountryProxy.LOAD_COUNTRY_COMPLETE:
					Variables.country = CountryProxy(note.getBody()).country;
					break;
				default:
					trace(MainMediator.NAME + " 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}


		private function storeProfile(proxy:MyInfoProxy):void
		{
			Variables.myPernr 		= proxy.pernr;
			Variables.myName		= proxy.profile.ENAME
			Variables.myOrgCode		= proxy.profile.ORGEH
			Variables.myOrg			= proxy.profile.ORGTX
			Variables.myBossCode	= proxy.profile.CFPERNR
			Variables.myBossName	= proxy.profile.CFPERNR
			Variables.authorities 	= proxy.authority;
			Variables.myPosition	= proxy.profile.PNAME;
			Variables.myHwtop		= proxy.profile.HWTOP;
			
			Variables.myKostl		= proxy.profile.KOSTL;
			Variables.myCardno		= proxy.profile.CARDNO;
			Variables.mySid			= proxy.profile.SID;
			
			Variables.myTel			= proxy.profile.TEL;
			
			//Variables.myEhrole		= proxy.authority.EHROLE;
			
			//view.vwMain.visible = true;			
			trace("succeed to get my prosfile: " + Variables.myName);
		}
		
		private function onErrorLoadModule(e:Event):void
		{
			facade.sendNotification(Messages.ACTIVATE_MODULE, this);			
		}
		
		private function onFailedLoadProfile(proxy:MyInfoProxy):void
		{
			CommonAlert.showMessage(commonFacade.bundle, "ALERT_FAILED_USERINFO", null, CommonAlert.TITLE_ERROR, view, closeApplication)
		}

		private function closeApplication():void
		{
			ExternalCommand.closeBrowser();
		}
		
		private var logoutView:AutoLogoutView;
		 
		private function onSafeTimeOut(e:TimerEvent):void
		{
			Variables.safeTimer.reset(); 
			if(logoutView==null) {
				logoutView = PopUpManager.createPopUp(view, AutoLogoutView, true) as AutoLogoutView;
				logoutView.addEventListener(AutoLogoutViewEvent.AUTOLOGOUT_VIEW_CLOSE, onAutoLogoutViewClosed);
			}
			else {
				PopUpManager.addPopUp(logoutView, view, true);
			}
			logoutView.startWaiting();
			PopUpManager.centerPopUp(logoutView);
			logoutView.setFocus();
			//CommonAlert.showMessage(commonFacade.bundle, "ALERT_FAILED_USERINFO", null, CommonAlert.TITLE_ERROR, view, closeApplication)
		}

		private function onAutoLogoutViewClosed(e:AutoLogoutViewEvent):void {
			if(e.close_type == AutoLogoutViewEvent.LOGOUT_CONFIRM) {
				closeApplication();
			}
			else {
				Variables.safeTimer.start();
			}
		}
		/*
		안전한 금융거래를 위하여 5분 동안 거래가 없을 경우
		자동으로 접속 종료됩니다.
		지금 로그인 시간을 연장하시겠습니까?
		*/
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////

        private function get view():eHRMain
        {
            return viewComponent as eHRMain;
        }
        
	}
}